#include "playsceen.h"
#include<QString>
#include<QMenuBar>
#include<QPainter>
#include<QLabel>
#include<QDebug>
#include<QPropertyAnimation>
#include<QSound>
#include "mypushbutton.h"
#include"mycoin.h"
#include "dataconfig.h"
PlaySceen::PlaySceen(int levelNum)
{
    this->levelIndex=levelNum;
    //初始化游戏场景
    //配置选择关卡场景
    this->setFixedSize(400,710);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    QString string=QString("翻金币第%1关").arg(levelIndex);
    this->setWindowTitle(string);

    //创建菜单栏
    QMenuBar *bar=menuBar();
    this->setMenuBar(bar);
    QMenu *startMenu=bar->addMenu("开始");
    //退出菜单项
    QAction *quitAction=startMenu->addAction("退出");
    //退出
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮音效
    QSound *backSound=new QSound(":/res/BackButtonSound.wav",this);
    //翻金币音效
    QSound *coinSound=new QSound(":/res/ConFlipSound.wav",this);
    //胜利音效
    QSound *winSound=new QSound(":/res/LevelWinSound.wav",this);

    //返回按钮
    MyPushButton *backBtn =new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    connect(backBtn,&MyPushButton::clicked,[=](){
        //播放返回按钮
        backSound->play();
        QTimer::singleShot(50,this,[=](){
            this->hide();
            emit this->ChooseSceneBack();
        });
    });

    QLabel *label=new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    QString str=QString("level: %1").arg(this->levelIndex);
    //字体设置标签控件中
    label->setFont(font);
    label->setText(str);
    label->setGeometry(30,this->height()-50,120,50);

    //初始化每个关卡的二维数组
    dataConfig data;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            this->gameArray[i][j]=data.mData[this->levelIndex][i][j];
        }
    }

    //胜利图片显示
    QLabel *winLabel=new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    //-:胜利才显示,开始先隐藏到窗口上方
    winLabel->move((this->width()-tmpPix.width())*0.5,-tmpPix.height());
    //显示金币背景图片
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            //绘制背景图片
            QPixmap pix =QPixmap(":/res/BoardNode.png");
            QLabel *label2=new QLabel;
            //设置label大小
            label2->setGeometry(0,0,pix.width(),pix.height());
            label2->setPixmap(pix);
            label2->setParent(this);
            label2->move(90+i*50,200+j*50);

            //创建金币
            QString str;
            if(this->gameArray[i][j]==1){
                //显示金币
                str=":/res/Coin0001.png";
            }
            else{
                //显示银币
                 str=":/res/Coin0008.png";
            }
            MyCoin *coin=new MyCoin(str);
            coin->setParent(this);
            coin->move(92+i*50,204+j*50);

            //给金币属性赋值
            coin->posX=i;
            coin->posY=j;
            coin->flag=gameArray[i][j];//1正面 0反面
            //将金币放入二维数组中
            this->coinBtn[i][j]=coin;
            //点击金币翻转
            connect(coin,&MyCoin::clicked,[=](){
                coinSound->play();
                //将全部按钮先禁用
                for(int i=0;i<4;i++){
                    for(int j=0;j<4;j++){
                        this->coinBtn[i][j]->isWin=true;
                    }
                }
                coin->changeFlag();
                this->gameArray[i][j]=this->gameArray[i][j]== 0?1:0;
                //翻转周围硬币
                //延时翻转
                QTimer::singleShot(100,this,[=](){
                    if(coin->posX+1<=3){
                        //翻转右侧金币
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX+1][coin->posY]=this->gameArray[coin->posX+1][coin->posY]== 0?1:0;
                    }
                    if(coin->posX-1>=0){
                        //翻转左侧金币
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX-1][coin->posY]=this->gameArray[coin->posX-1][coin->posY]== 0?1:0;
                    }
                    if(coin->posY+1<=3){
                        //翻转上面金币
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY+1]=this->gameArray[coin->posX][coin->posY+1]== 0?1:0;
                    }
                    if(coin->posY-1>=0){
                        //翻转下面金币
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY-1]=this->gameArray[coin->posX][coin->posY-1]== 0?1:0;
                    }
                    //按完周围金币后按钮全部解禁
                    for(int i=0;i<4;i++){
                        for(int j=0;j<4;j++){
                            this->coinBtn[i][j]->isWin=false;
                        }
                    }

                    //判断是否胜利
                    this->isWin=true;
                    for(int i=0;i<4;i++){
                        for(int j=0;j<4;j++){
                            if(coinBtn[i][j]->flag==false){//失败
                                    this->isWin=false;
                                    break;
                                }
                        }
                    }
                    if(this->isWin==true){//胜利
                        winSound->play();
                        qDebug()<<"胜利";
                        //将全部按钮的胜利标志改为true,无法再次点击按钮
                        for(int i=0;i<4;i++){
                            for(int j=0;j<4;j++){
                                 coinBtn[i][j]->isWin=true;
                            }
                        }
                        //将胜利图片拿下来
                        QPropertyAnimation *animation=new QPropertyAnimation(winLabel,"geometry");
                        //设置时间间隔
                        animation->setDuration(800);
                        //设置开始位置
                        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        //设置结束位置
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y()+150,winLabel->width(),winLabel->height()));
                        //设置缓和曲线
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        //执行动画
                        animation->start();
                    }
                });
            });
        }
    }

}

void PlaySceen::paintEvent(QPaintEvent *event)
{
    //创建背景
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //画背景上的图标
    pix.load(":/res/Title.png");
    pix=pix.scaled(pix.width()*0.6,pix.height()*0.6);
    painter.drawPixmap(10,30,pix.width(),pix.height(),pix);



}
